<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Model\Type;
use DB;

class TypeController extends Controller
{
	protected $Type;
	public function __construct()
	{
		$this->Type = new Type();
	}
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
    	//获取数据
    	$data = $this->Type->findAll();
//     	dd($data);
        return view('admin.type_index')->with('data',$data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
    	$data = $this->Type->findAll();
        return view('admin.type_add',['data'=>$data]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
    	//验证
    	$this->validate($request, [
    		'name'        =>      'required',
    		'pid'            =>      'required',	
    	],[
    		'required'    =>      ':attribute  是必须的',
    	]);
    	
    	$type = new Type();
        $type->name =  $request->name;
        $type->pid    =  $request->pid;
        $type->save();
        return redirect('/admin/type');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $res = DB::table('type')->where('id',$id)->first();
        return view('admin.type_edit',['res'=>$res]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
       	$this->validate($request, [
    		'name'        =>      'required',
    	],[
    		'required'    =>      ':attribute  是必须的',
    	]);
    	
    	$type = new Type();
        $type->name =  $request->name;
        $type->save();
        return redirect('/admin/type');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    
    /**
     * 删除单个栏目
     */
    public function  delete(Request $request)
    {
    	$id = $request->id;
     	$data = DB::table('type')->get();
    	/* foreach($data as $v){
	    	if($v->pid == $id){
	    		$res = DB::table('type')->where('pid',$id)->delete();
	    		$res = DB::table('type')->where('id',$id)->delete();
	    	}
	    	return response()->json($res);
    	}  */
    	$res = DB::table('type')->where('id',$id)->delete();
    	return response()->json($res);
    }

}
